package cn.dnuihyy406.dnuifood_16110100406.controller.homefragment;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import java.util.List;

import cn.dnuihyy406.dnuifood_16110100406.R;
import cn.dnuihyy406.dnuifood_16110100406.bean.ShopBean;

import cn.dnuihyy406.dnuifood_16110100406.controller.foodlist.FoodListActivity;
import cn.dnuihyy406.dnuifood_16110100406.model.BaseModel;


public class ShopAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements
        View.OnClickListener{
    private Context mContext;
    private List<ShopBean> shopsList1;
    private LayoutInflater mLayoutInflater;
    //    private OnRecyclerViewItemClickListenr mOnItemClickListener = null;
    public ShopAdapter (Context mContext,List shopsList1){
        this.mContext = mContext;
        this.shopsList1=shopsList1;
        mLayoutInflater=LayoutInflater.from(mContext);
    }

    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View v=mLayoutInflater.from(viewGroup.getContext()).inflate(R.layout.shop_item,viewGroup,false);
        v.setOnClickListener(this);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder Holder, final int i) {
        final ShopBean x=shopsList1.get(i);
        if (null==x){
            return;
        }
        ViewHolder viewHolder= (ViewHolder)Holder;
        viewHolder.bar.setRating(x.getLevel());
        viewHolder.address.setText(x.getAddress());
        viewHolder.title.setText(x.getShopname());
//        String Base_url ="http://172.24.10.175:8080/foodService/";
        Picasso.with(mContext).load(BaseModel.BaseUrl+x.getPic()).into(viewHolder.image);
//        viewHolder.itemView.setTag(x);
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String str = String.valueOf(shopsList1.get(i).getShop_id());
                String str1 = String.valueOf(shopsList1.get(i).getShopname());
                Intent intent=new Intent(mContext, FoodListActivity.class);
                intent.putExtra("shop_id",str);
                intent.putExtra("shopname",str1);
                mContext.startActivity(intent);
                Toast.makeText(mContext,x.getShopname(),Toast.LENGTH_SHORT).show();

            }
        });
    }



    @Override
    public int getItemCount() {
        return shopsList1.size();
    }
    ////
    @Override
    public void onClick(View view) {
//        if(mOnItemClickListener!=null){
//            //使用getTag方法获取数据
//            mOnItemClickListener.onItemClick(view,view.getTag());
//        }
    }

    //listener   interface
//    public interface OnRecyclerViewItemClickListenr{
//        void onItemClick(View view,Object data);
//    }
//    public void setOItemClickListener(OnRecyclerViewItemClickListenr listener) {
//        this.mOnItemClickListener=listener;
//    }



    public class ViewHolder extends RecyclerView.ViewHolder {
        RatingBar bar;
        TextView title,address;
        ImageView image;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            bar= itemView.findViewById(R.id.shop_ratingBar);
            title = itemView.findViewById(R.id.food_name);
            address = itemView.findViewById(R.id.shop_address);
            image = itemView.findViewById(R.id.food_pic);
        }
    }
}